package com.tiennt.android.callnotes.models.listadapters;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.tiennt.android.callnotes.EditNotesActivity;
import com.tiennt.android.callnotes.R;
import com.tiennt.android.callnotes.ViewNotesActivity;
import com.tiennt.android.callnotes.ViewNotesByContactActivity;
import com.tiennt.android.callnotes.models.contentholders.ListNotesItem;
import com.tiennt.android.callnotes.models.viewholders.ListNotesItemViewHolder;
import com.tiennt.android.callnotes.utils.ProjectUtils;

public class ListNotesAdapter extends BaseAdapter {
	private Activity mActivity;
	private ArrayList<ListNotesItem> mListItems;
	private ArrayList<Integer> mSelectedItems = new ArrayList<Integer>();
	private OnCheckedChangeListener onCheckedChangeListener = null;

	private final String TAG = "ListNotesAdapter";
	private final boolean DBG = true;

	public ListNotesAdapter(Activity activity, ArrayList<ListNotesItem> objects) {
		// TODO Auto-generated constructor stub
		mActivity = activity;
		mListItems = objects;
	}

	public void setListData(ArrayList<ListNotesItem> data) {
		mListItems = data;
	}

	public void setNewSelection(int position) {
		if (!mSelectedItems.contains(position)) {
			mSelectedItems.add(position);
			notifyDataSetChanged();
		}
	}

	public boolean isPositionChecked(int position) {
		return mSelectedItems.contains(position);
	}

	public ArrayList<Integer> getCurrentCheckedPosition() {
		return mSelectedItems;
	}

	public void removeSelection(int position) {
		if (mSelectedItems.contains(position)) {
			mSelectedItems.remove(mSelectedItems.indexOf(position));
			notifyDataSetChanged();
		}
	}

	public void clearSelection() {
		mSelectedItems.clear();
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListItems.size();
	}

	@Override
	public ListNotesItem getItem(int position) {
		// TODO Auto-generated method stub
		return mListItems.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
		ListNotesItemViewHolder viewHolder = null;

		if (v == null) {
			v = LayoutInflater.from(mActivity).inflate(R.layout.list_notes_row,
					null);
			viewHolder = new ListNotesItemViewHolder(v);
			v.setTag(viewHolder);
		} else {
			viewHolder = (ListNotesItemViewHolder) v.getTag();
		}

		final ListNotesItem item = mListItems.get(position);

		viewHolder.getChbxSelect().setOnCheckedChangeListener(
				new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) {
							setNewSelection(position);
						} else {
							removeSelection(position);
						}
						onCheckedChangeListener.onCheckedChanged(buttonView,
								isChecked);
					}
				});

		viewHolder.getLinearContact().setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Bundle bundle = new Bundle();
						bundle.putLong(ProjectUtils.BUNDLEKEY_CONTACTID,
								Long.parseLong(item.getContactId()));
						bundle.putString(ProjectUtils.BUNDLEKEY_CONTACTNAME,
								item.getContactName());
						ByteArrayOutputStream bs = new ByteArrayOutputStream();
						item.getContactPhoto().compress(
								Bitmap.CompressFormat.PNG, 100, bs);

						Intent iNotesByContact = new Intent(mActivity,
								ViewNotesByContactActivity.class);
						iNotesByContact.putExtras(bundle);
						iNotesByContact.putExtra(
								ProjectUtils.BUNDLEKEY_CONTACTPHOTO,
								bs.toByteArray());
						mActivity.startActivityForResult(iNotesByContact,
								ViewNotesActivity.REQUEST_ADD_OR_EDIT_NOTE);
					}
				});

		viewHolder.getRelativeNote().setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent iEditNote = new Intent(mActivity,
								EditNotesActivity.class);
						Bundle data = new Bundle();
						data.putString(ProjectUtils.BUNDLEKEY_CONTACTNAME,
								item.getContactName());
						data.putString(ProjectUtils.BUNDLEKEY_NOTETITLE,
								item.getNoteTitle());
						data.putString(ProjectUtils.BUNDLEKEY_NOTECONTENT,
								item.getNoteContent());
						data.putInt(ProjectUtils.BUNDLEKEY_ITEMID, item.getId());
						data.putBoolean(ProjectUtils.BUNDLEKEY_INCOMING,
								item.getInStatus());
						data.putBoolean(ProjectUtils.BUNDLEKEY_OUTGOING,
								item.getOutStatus());
						data.putString(ProjectUtils.BUNDLEKEY_CONTACTID,
								item.getContactId());

						iEditNote.putExtras(data);
						mActivity.startActivityForResult(iEditNote,
								ViewNotesActivity.REQUEST_ADD_OR_EDIT_NOTE);
					}
				});

		if (item != null) {
			viewHolder.getImgvContactPhoto().setImageBitmap(
					item.getContactPhoto());
			viewHolder.getTvNoteTitle().setText(item.getNoteTitle());
			viewHolder.getTvContactName().setText(item.getContactName());
			viewHolder.getChbxIn().setChecked(item.getInStatus());
			viewHolder.getChbxOut().setChecked(item.getOutStatus());

			if (mSelectedItems.contains(position)) {
				viewHolder.getChbxSelect().setChecked(true);
				v.setBackgroundColor(mActivity.getResources().getColor(
						android.R.color.holo_blue_light));
			} else {
				viewHolder.getChbxSelect().setChecked(false);
				v.setBackgroundColor(mActivity.getResources().getColor(
						android.R.color.background_light));
			}
		}

		return v;
	}

	public void setOnCheckedChangeListener(
			OnCheckedChangeListener onCheckedChangeListener) {
		this.onCheckedChangeListener = onCheckedChangeListener;
	}

	public void removeAt(int index) {
		if (index >= mListItems.size()) {
			return;
		}
		mListItems.remove(index);
		super.notifyDataSetChanged();
	}

	public int getSelectedCount() {
		return mSelectedItems.size();
	}

	public void log(String msg) {
		if (DBG) {
			Log.d(TAG, msg);
		}
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
}
